package com.lheia.tool

import generate.annotation.Column
import generate.annotation.Table

class ToolClass {
    /**
     * 返回Domain类解析信息
     * @param classPath 类路径
     * @return
     */
    static Map parseDomainClass(String classPath){
        Class domainClass =Class.forName(classPath)
        return parseDomainClass(domainClass)
    }

    /**
     * 返回Domain类解析信息
     * @param aClass
     * @return
     */
    static Map parseDomainClass(Class domainClass){
        Table table = domainClass.getAnnotation(Table.class)
        def property =[:]
        property.domainName = domainClass.getSimpleName()
        property.lowDomainName = ToolString.toLowerCaseFirstOne(property.domainName)
        property.domainNameCN =  table.nameCN()
        property.packageName = domainClass.getPackage().getName()
        property.columnMap = [:]
        property.query = []
        property.table = []
        property.first = []
        property.second = []
        domainClass.getDeclaredFields().eachWithIndex {item,index->
            Column column = item.getAnnotation(Column.class)
            if(column){
                def dataMap = [:]
                dataMap.tpye = item.getType()
                dataMap.name = item.getName()
                dataMap.nameCN = column.nameCN()
                //***是否放入列表中**/
                if(column.isInTable()){
                    property.table << [name:item.getName(),nameCN:column.nameCN(),width:"20%"]
                }
                /***
                 * 是否当做查询条件
                 */
                if(column.isQuery()){
                    property.query << [queryName:item.getName(),queryNameCN:column.nameCN()]
                }
                if(index%2){
                    property.second<< [name:item.getName(),nameCN:column.nameCN(),required:(column.isRequired()?'lay-verify="required"':""),requiredSign:(column.isRequired()?'<span class="col-f00">* </span>':"")]
                }else{
                    property.first<< [name:item.getName(),nameCN:column.nameCN(),required:(column.isRequired()?'lay-verify="required"':""),requiredSign:(column.isRequired()?'<span class="col-f00">* </span>':"")]
                }
                dataMap.isInTable = column.isInTable()
                dataMap.isQuery = column.isQuery()
                property.columnMap.put(dataMap.name,dataMap)
            }
        }
        return property
    }
}
